package cn.example.web.config;

import cn.example.web.interceptor.ExceptionInterceptor;
import cn.example.web.plugins.storage.StoragePluginFactory;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.setting.Setting;
import com.jfinal.config.*;
import com.jfinal.template.Engine;
import org.artifact.core.context.id.SnowflakeFactory;
import org.artifact.core.lang.IContext;
import org.artifact.core.lang.IServer;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

/**
 * @author : SandKing
 * @CreateDate : 2019/12/9 16:05
 * @Description ：Please describe this document
 */
public class WebConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        IContext context = IServer.me().getContext();

        // 初始化Spring
        context.setSpringContext(new ClassPathXmlApplicationContext("app-init.xml"));

        // jfinal交由Spring注入
        me.setControllerFactory(new ControllerFactory());

        // 设置配置文件
        context.setSetting(new Setting("app.properties"));

        // 设置数据源
        context.setDataSource(context.getSpringContext().getBean("dataSource", DataSource.class));

        // 设置ID工厂
        context.setIdFactory(new SnowflakeFactory(new Snowflake(0, 0)));
    }

    @Override
    public void configRoute(Routes me) {
        me.add(new RoutesScan());
    }

    @Override
    public void configEngine(Engine me) {

    }

    @Override
    public void configPlugin(Plugins me) {
        // 数据存储插件
        me.add(new StoragePluginFactory().create());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        // 异常拦截器
        me.add(new ExceptionInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {

    }
}
